package com.jinbo.footmessage.httpretrofit;




import android.util.Log;

import com.jinbo.footmessage.httpbean.BaseBean;
import com.jinbo.footmessage.httpbean.CommentBean;
import com.jinbo.footmessage.httpbean.OrderBean;
import com.jinbo.footmessage.httpbean.OrdersBean;
import com.jinbo.footmessage.httpbean.UserInformationBean;
import com.jinbo.footmessage.httpbean.SendCodeBean;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.httpbean.WorkersBean;
//import com.jinbo.footmessage.utils.BitmapUtils;

import java.io.File;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.mime.TypedFile;


public class BusinessController {

    private HttpBusinessStore mRemoteBusinessStore;

    private BusinessController() {
    }

    public static BusinessController getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        public static final BusinessController INSTANCE = new BusinessController();
    }

    public HttpBusinessStore getRemoteBusinessStore() {
        if (mRemoteBusinessStore == null) {
            mRemoteBusinessStore = new HttpBusinessStore();
        }
        return mRemoteBusinessStore;
    }

    /**
     * 申请发送手机验证码
     *
     * @param mobile   手机号
     * @param listener
     */
    public void sendCode(final String mobile, final Listener<SendCodeBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().sendCode(mobile,
                    new Callback<BaseBean<SendCodeBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<SendCodeBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 用户注册
     *
     * @param mobile   手机号
     * @param password 密码
     * @param name
     * @param listener
     */
    public void register(final String mobile,  final String password, final String name, final String sex,final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().register(mobile, password, name, sex,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 登录
     *
     * @param mobile
     * @param password
     * @param listener
     */
    public void login(final String mobile, final String password, final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().login(mobile, password,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 找回密码
     *  @param mobile   手机
     * @param password 密码
     * @param listener
     */
    public void findPassword(final String mobile, final String password, final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().findPassword(mobile, password,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 修改头像
     *
     * @param user_id //  令牌
     * @param imgPath      // 图片路径
     * @param kind
     * @param listener
     */
    public void modifyFace(final String user_id, final String imgPath,final int kind, final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            String tempImagePath = imgPath;
            if (tempImagePath.startsWith("file://")) {
                tempImagePath = imgPath.replace("file://", "");
            }
            final File file =new File(tempImagePath);// BitmapUtils.getCompressImage(tempImagePath);
            //Log.i("Gunter", "file:" + file.getPath());
            getRemoteBusinessStore().modifyFace(user_id, new TypedFile("image/png", file), kind,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 修改用户名
     *
     * @param user_id // // 令牌
     * @param new_name         // 新的名字
     * @param listener
     */
    public void modifyUserName(final String user_id, final String new_name, final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().modifyUserName(user_id, new_name,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 修改用户性别
     *@param user_id
     * @param sex      性别
     * @param listener
     */
    public void modifyUserSex(final String user_id, final String sex, final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().modifyUserSex(user_id, sex,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 修改用户邮箱
     *
     * @param user_id 令牌
     * @param mail        用户邮箱
     * @param listener
     */
    public void modifyUserEmail(final String user_id, final String mail, final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {


            getRemoteBusinessStore().modifyUserEmail(user_id, mail,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 修改密码
     *
     * @param user_id 令牌
     * @param old_password 旧密码
     * @param new_password 新密码
     * @param listener
     */
    public void modifyUserPassword(final String user_id, final String old_password, final String new_password, final Listener<UserInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().modifyUserPassword(user_id, old_password, new_password,
                    new Callback<BaseBean<UserInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<UserInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }


    /**
     * 查看所有技师
     *
     *
     * @param listener
     */
    public void LookWorkers(final Listener<WorkersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().LookWorkers(
                    new Callback<BaseBean<WorkersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 获取我的项目
     *@param user_id
     *
     */
    public void getMyProjects(final String user_id,final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().getMyProjects(user_id,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 获取技师的评论
     *@param worker_id
     *
     */
    public void getComments(final String worker_id,final int start,final int limit,final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().getComments(worker_id, start, limit,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 预约订单
     */
    public void MakeOrder( final String date,
                           final String week,
                           final String begin_time,
                           final String end_time,
                           final String content,
                           final String total_price,
                           final  String worker_id,
                           final String user_id,
                           final String contact_name,
                           final String contact_address,
                           final String contact_phone,
                           final String contact_sex,
                           final String order_time,
                           final Listener<OrderBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().MakeOrder(
                    date,
                    week,
                    begin_time,
                    end_time,
                    content,
                    total_price,
                    worker_id,
                    user_id,
                    contact_name,
                    contact_address,
                    contact_phone,
                    contact_sex,
                    order_time,
                    new Callback<BaseBean<OrderBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrderBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 查询待确认订单
     *@param user_id
     *
     */
    public void NotConfirmOrder(final String user_id,final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().NotConfirmOrder(user_id,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 用户取消待确认的单
     *@param id
     *
     */
    public void CancelOrder(final int id,final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().CancelOrder(id,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 查询确认订单
     *@param user_id
     *
     */
    public void confirmOrder(final String user_id,final int start,final int limit,final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().confirmOrder(user_id, start, limit,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
    /**
     * 评论订单
     *
     *
     */
    public void commentOrder(final int id,final String comment,final double star,final  String time,final Listener<CommentBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().commentOrder(id, comment, star, time,
                    new Callback<BaseBean<CommentBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<CommentBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

}
